package com.ktjy.rabbitmqconsumer7002.utils;


import com.alibaba.fastjson2.JSON;
import com.ktjy.pojo.UserDTO;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class MqConsumer {

    /**
     * 监听简单队列信息
     *
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue("spring_simple_queue"))
    public void springSimpleQueueListener(String msg) {
        System.out.println("7002服务:springSimpleQueueListener 收到消息:" + msg);
    }

    @RabbitListener(queuesToDeclare = @Queue("spring_work_queue"), concurrency = "3")
    public void springWorkQueueListener(String msg) {
        System.out.println("7002服务:workQueueListener收到消息:" + msg + ",当前处理线程:"
                + Thread.currentThread().getName());
    }

    // -------------发布订阅模式 start-------------

    /**
     * 短信消费者监听消息
     */
    @RabbitListener(queuesToDeclare = @Queue("sms_queue"))
    public void smsQueueListener(String msg) {
        // 解析数据
        UserDTO userInfo = JSON.parseObject(msg, UserDTO.class);
        System.out.println("收到用户信息，正在发送短信到："
                + userInfo.getPhone());
    }

    /**
     * 邮箱消费者监听消息
     */
    @RabbitListener(queuesToDeclare = @Queue("email_queue"))
    public void emailQueueListener(String msg) {
        // 解析数据
        UserDTO userInfo = JSON.parseObject(msg, UserDTO.class);
        System.out.println("收到用户信息，正在发送邮件到："
                + userInfo.getEmail());
    }
    // -------------发布订阅模式 end-------------
}
